<?php

	$action = fgsBase::getParam(0);
	$id = (int)fgsBase::getParam(1);
	$form = null;

	// ---------------------------------------------------------------
	// ADD or EDIT a command 
	// ---------------------------------------------------------------
	if ($action == 'add' || $action == 'edit') {
		$form = new htmlForm('command');

		if ($form->isCancel())
			fgsBase::redirect('admin_commands');
		
		if ($action == 'add') {
			$form->addSection(tr('Add a command'));
		} else {
			$command = fgsCommands::getById($id);
			if (!$command)
				fgsBase::redirect('admin_commands');
			$form->addSection(sprintf(tr('Edit command #%d'), $id));

			$form->default_values['gid'] = $command['gid'];
			$form->default_values['name'] = $command['name'];
			$form->default_values['bin_name'] = $command['bin_name'];
			$form->default_values['version'] = $command['version'];
			$form->default_values['os'] = $command['os'];
			$form->default_values['command'] = $command['command'];
			$form->default_values['help'] = $command['help'];
			$form->default_values['is_activated'] = $command['is_deleted'] ? 0 : 1;
		}

		$form->addField(tr('Gid'), 'gid', 'text', true);
		$form->addField(tr('Name'), 'name', 'text', true);
		$form->addField(tr('Binary name'), 'bin_name', 'text', true);
		$form->addField(tr('Version'), 'version', 'text', true);
		$form->addField(tr('OS'), 'os', 'text', true);
		$form->addField(tr('Command'), 'command', 'text', true);
		$form->addField(tr('Help'), 'help', 'textarea');
		$form->addField(tr('Activate'), 'is_activated', 'checkbox');
		$form->addSubmit(tr('Submit'));

		// add command if post is good
		if ($form->isPost() && $form->isValid()) {
			$gid = $form->get('gid');
			$name = $form->get('name');
			$bin_name = $form->get('bin_name');
			$version = $form->get('version');
			$os = $form->get('os');
			$command = $form->get('command');
			$help = $form->get('help');
			$is_deleted = $form->get('is_activated') ? false : true;
			
			if ($action == 'add')
				fgsCommands::add($gid, $name, $bin_name, $version, $os, $command, $help, $is_deleted);
			else 
				fgsCommands::update($id, $gid, $name, $bin_name, $version, $os, $command, $help, $is_disabled);

			fgsBase::redirect('admin_commands');
		}


	// ---------------------------------------------------------------
	// SHOW a command
	// ---------------------------------------------------------------
	} else if ($action == 'show') {
		fgsCommands::enable($id, 1);
		fgsBase::redirect('admin_commands');
		
	// ---------------------------------------------------------------
	// HIDE a command
	// ---------------------------------------------------------------
	} else if ($action == 'hide') {
		fgsCommands::enable($id, 0);
		fgsBase::redirect('admin_commands');

	// ---------------------------------------------------------------
	// RETRIEVE a command
	// ---------------------------------------------------------------
	} else if ($action == 'retrieve') {
		if (fgsCommands::retrieve())
			fgsBase::redirect('admin_commands');
	}
	
?>
